package com.lynn.common.utils.file;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.math.BigDecimal;

/**
 * Desc:   文件工具类
 */

public class FileCacheUtil {

    private final String TAG = this.getClass().getName();

// *************************************************************************
//  手机缓存大小计算
// *************************************************************************

    /**
     * 计算缓存目录（内外）的缓存大小
     */
    public static String getCacheDirSize(Context context) {
        // 内部缓存目录
        long cacheSize = FileUtil.getFilesSize(getCacheDirI(context));
        // 外部缓存目录
        if (checkSDCard()) {
            cacheSize += FileUtil.getFilesSize(getCacheDirE(context));
        }
        return getFormatSize(cacheSize);
    }

    /**
     * 格式化单位
     */
    private static String getFormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return "0K"; // size + "B"
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "K";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "M";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "G";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
                + "T";
    }

    /**
     * 清除缓存目录（内外）的缓存
     */
    public static void clearCacheDir(Context context) {
        // 清除内部缓存
        FileUtil.deleteFile(getCacheDirI(context));
        // 清除外部缓存
        if (checkSDCard()) {
            FileUtil.deleteFile(getCacheDirE(context));
        }
    }
// *************************************************************************
// 手机内部存储文件 START
// android 4.4及以上系统不需要申请SD卡读写权限,应用卸载后会自动清除
// *************************************************************************

    /**
     * /data/data/app_package_name/cache
     */
    public static File getCacheDirI(Context context) {
        return context.getCacheDir();
    }

    /**
     * /data/data/app_package_name/files
     */
    public static File getFileDirI(Context context) {
        return context.getFilesDir();
    }

    /**
     * /data/data/com.xx.xx/files
     * /data/data/com.xx.xx/files/path
     */
    public static File getFileDirI(Context context, String path) {
        if (TextUtils.isEmpty(path)) {
            return context.getFileStreamPath("");
        }
        return context.getFileStreamPath(path);
    }

    /**
     * /data/data/com.xx.xx/app_name
     */
    public static File getAppDirI(Context context, String name) {
        return context.getDir(name, Context.MODE_PRIVATE);
    }

// *************************************************************************
//  手机外部存储文件
//   需要读写权限
// *************************************************************************

    /**
     * storage/emulated/0/Android/data/app_package_name/cache
     */
    public static File getCacheDirE(Context context) {
        if (!checkSDCard()) return null;
        File cacheDir = context.getExternalCacheDir();
        //有些手机需要通过自定义目录
        if (cacheDir == null) {
            // 外部存储根目录（公共） /storage/emulated/0
            File file = Environment.getExternalStorageDirectory();
            String customDir = "Android/data/" + context.getPackageName() + "/cache";
            cacheDir = new File(file, customDir);
        }
        return cacheDir;
    }

    /**
     * /storage/emulated/0/Android/data/com.xx.xx/files/Music
     *
     * @param context
     * @param type    {@link Environment#DIRECTORY_MUSIC},
     *                {@link Environment#DIRECTORY_PODCASTS},
     *                {@link Environment#DIRECTORY_RINGTONES},
     *                {@link Environment#DIRECTORY_ALARMS},
     *                {@link Environment#DIRECTORY_NOTIFICATIONS},
     *                {@link Environment#DIRECTORY_PICTURES}, or
     *                {@link Environment#DIRECTORY_MOVIES}.or
     *                自定义文件夹名称
     */
    public static File getFileDirE(Context context, String type) {
        if (!checkSDCard()) return null;
        File fileDir = null;
        if (TextUtils.isEmpty(type))
            fileDir = context.getExternalFilesDir("");
        else
            fileDir = context.getExternalFilesDir(type);

        //有些手机需要通过自定义目录
        if (fileDir == null) {
            // 外部存储根目录（公共） /storage/emulated/0
            File file = Environment.getExternalStorageDirectory();
            String customDir = "Android/data/" + context.getPackageName() + "/files" +
                    (TextUtils.isEmpty(type) ? "" : "/" + type);
            fileDir = new File(file, customDir);
        }
        return fileDir;
    }

    /**
     * 获取外部存储公共目录
     *
     * @param type {@link Environment#DIRECTORY_MUSIC},
     *             {@link Environment#DIRECTORY_PODCASTS},
     *             {@link Environment#DIRECTORY_RINGTONES},
     *             {@link Environment#DIRECTORY_ALARMS},
     *             {@link Environment#DIRECTORY_NOTIFICATIONS},
     *             {@link Environment#DIRECTORY_PICTURES}, or
     *             {@link Environment#DIRECTORY_MOVIES}.or 自定义文件夹名称
     */
    public static File getPubDirE(String type) {
        if (!checkSDCard()) return null;
        File cacheDir = null;

        if (TextUtils.isEmpty(type)) {
            cacheDir = Environment.getExternalStorageDirectory();
        } else {
            cacheDir = Environment.getExternalStoragePublicDirectory(type);
        }


        return cacheDir;
    }


    private static boolean checkSDCard() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }



}
